/*
 * Copyright (c) 2012 by Fernando Correa da Conceição
 *
 * This is released under MIT license, read the file LICENSE.txt or
 * http://opensource.org/licenses/mit-license.php .
 *
 * Some parts of this file are generated from form editor in netbeans.
 */
package info.jaguaribe.jaguaribe;

import java.util.ArrayList;
import java.util.ResourceBundle;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;

/**
 * The frame to create a new Slideshow.
 *
 * @author Fernando Correa da Conceição
 */
public class SlideshowFrame extends javax.swing.JDialog {
    /**
     * Used for class serialization.
     */
    private static final long serialVersionUID = 1L;

    /**
     * The model used in the table.
     */
    private DefaultTableModel model = null;

    /**
     * The name of the file for this slideshow.
     */
    private String filename = "";

    /**
     * Creates new form SlideshowFrame
     *
     * @param parent The parent frame
     * @param modal true if this is a modal dialog
     */
    public SlideshowFrame(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.initComponents();
        this.postInitComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        addButton = new info.jaguaribe.common.ButtonChooseFile();
        removeButton = new javax.swing.JButton();
        moveUpButton = new javax.swing.JButton();
        moveDownButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        loadButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        randomItemsCheckbox = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        table.setModel(this.createTableModel());
        jScrollPane1.setViewportView(table);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("info/jaguaribe/jaguaribe/Bundle"); // NOI18N
        addButton.setText(bundle.getString("SlideshowFrame.addButton.text")); // NOI18N

        removeButton.setText(bundle.getString("SlideshowFrame.removeButton.text")); // NOI18N
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        moveUpButton.setText(bundle.getString("SlideshowFrame.moveUpButton.text")); // NOI18N
        moveUpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                moveUpButtonActionPerformed(evt);
            }
        });

        moveDownButton.setText(bundle.getString("SlideshowFrame.moveDownButton.text")); // NOI18N
        moveDownButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                moveDownButtonActionPerformed(evt);
            }
        });

        saveButton.setText(bundle.getString("SlideshowFrame.saveButton.text")); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        loadButton.setText(bundle.getString("SlideshowFrame.loadButton.text")); // NOI18N
        loadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadButtonActionPerformed(evt);
            }
        });

        okButton.setText(bundle.getString("SlideshowFrame.okButton.text")); // NOI18N
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText(bundle.getString("SlideshowFrame.cancelButton.text")); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        randomItemsCheckbox.setText(bundle.getString("SlideshowFrame.randomItemsCheckbox.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(59, 59, 59)
                        .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(randomItemsCheckbox, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE))
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(moveDownButton, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(moveUpButton, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(removeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(loadButton, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(40, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {addButton, loadButton, moveDownButton, moveUpButton, removeButton, saveButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(removeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(moveUpButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(moveDownButton, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(loadButton))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addComponent(randomItemsCheckbox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 177, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {addButton, loadButton, moveDownButton, moveUpButton, removeButton, saveButton});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Actions after the GUI is build
     */
    private void postInitComponents() {
        this.addButton.setTable(table, model);
    }

    /**
     * Remove the current selected line.
     *
     * When the user click on "Remove" button.
     *
     * @param evt The button click event.
     */
    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
        int line = this.table.convertRowIndexToModel(this.table.getSelectedRow());
        if (line > -1) {
            this.model.removeRow(line);
            this.table.revalidate();
        }
    }//GEN-LAST:event_removeButtonActionPerformed

    /**
     * Move the current select line one position up.
     *
     * When the user click on "Move Up" button.
     *
     * @param evt The button click event.
     *
     * @todo Make this work for multiple selected lines.
     */
    private void moveUpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveUpButtonActionPerformed
        int line = this.table.convertRowIndexToModel(this.table.getSelectedRow());
        if (line > 0) {
            this.model.moveRow(line, line, line - 1);
            this.table.getSelectionModel().setSelectionInterval(line - 1, line - 1);
            this.table.revalidate();
        }
    }//GEN-LAST:event_moveUpButtonActionPerformed

    /**
     * Move the current selected line one position down.
     *
     * When the user click on "Move Down" button.
     *
     * @param evt The button click event.
     *
     * @todo Make this work for multiple selected lines.
     */
    private void moveDownButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveDownButtonActionPerformed
        int line = this.table.convertRowIndexToModel(this.table.getSelectedRow());
        if (line > -1 && line < (this.model.getRowCount() - 1)) {
            this.model.moveRow(line, line, line + 1);
            this.table.getSelectionModel().setSelectionInterval(line + 1, line + 1);
            this.table.revalidate();
        }
    }//GEN-LAST:event_moveDownButtonActionPerformed

    /**
     * Save the slideshow.
     *
     * When the user click on "Save" button.
     *
     * @param evt The button click event.
     */
    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        ArrayList<String> paths = new ArrayList<String>();
        for (int i = 0; i < this.model.getRowCount(); i++) {
            paths.add(table.getModel().getValueAt(i, 0).toString());
        }
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Slideshow Files(*.sld)", "sld");
        JFileChooser fc = new JFileChooser();
        fc.addChoosableFileFilter(filter);
        int returnVal = fc.showSaveDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            Slideshow slideshow = new Slideshow();
            slideshow.setFile(fc.getSelectedFile().getAbsolutePath());
            slideshow.setItems(paths);
            slideshow.save();
            this.filename = fc.getSelectedFile().getAbsolutePath();
        }
    }//GEN-LAST:event_saveButtonActionPerformed

    /**
     * Load a slideshow from disk.
     *
     * When the user click on "Load" button.
     *
     * @param evt The button click event.
     */
    private void loadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadButtonActionPerformed
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Slideshow Files(*.sld)", "sld");
        JFileChooser fc = new JFileChooser();
        fc.addChoosableFileFilter(filter);
        int returnVal = fc.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            Slideshow slideshow = new Slideshow();
            slideshow.setFile(fc.getSelectedFile().getAbsolutePath());
            slideshow.load();
            ArrayList<String> items = slideshow.getItems();
            this.model.getDataVector().removeAllElements();
            for (String item : items) {
                this.model.addRow(new Object[]{item});
            }
            this.filename = fc.getSelectedFile().getAbsolutePath();
        }
    }//GEN-LAST:event_loadButtonActionPerformed

    /**
     * Start to show the slideshow.
     *
     * When the user click on "Ok" button.
     *
     * @param evt The button click event.
     */
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        ArrayList<String> paths = new ArrayList<String>();
        for (int i = 0; i < this.model.getRowCount(); i++) {
            paths.add(table.getModel().getValueAt(i, 0).toString());
        }
        Slideshow slideshow = new Slideshow();
        slideshow.randomItems = this.randomItemsCheckbox.isSelected();
        slideshow.setItems(paths);
        slideshow.createItems();
        slideshow.setFile(this.filename);
        Jaguaribe.getInstance().getFrame().setSlideshow(slideshow);
        this.dispose();
    }//GEN-LAST:event_okButtonActionPerformed

    /**
     * Cancel the slideshow.
     *
     * When the user click on "Cancel" button.
     *
     * @param evt The button click event.
     */
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    /**
     * Create a model to use in the table.
     *
     * @return The model to use in the table.
     */
    private DefaultTableModel createTableModel() {
        ResourceBundle msg = Jaguaribe.getInstance().getMessages();
        this.model = new DefaultTableModel();
        this.model.addColumn(msg.getString("SlideShowFrame.Table.Column.Path"));
        this.table.revalidate();
        return model;
    }
    //@cond
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private info.jaguaribe.common.ButtonChooseFile addButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton loadButton;
    private javax.swing.JButton moveDownButton;
    private javax.swing.JButton moveUpButton;
    private javax.swing.JButton okButton;
    private javax.swing.JCheckBox randomItemsCheckbox;
    private javax.swing.JButton removeButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JTable table;
    // End of variables declaration//GEN-END:variables
    //@endcond
}
